//*****************************************************************************
//**************** (C) 2007 by Eleven Engineering Incorporated ****************
//*****************************************************************************
//**
//**		Tabs:	This file looks best with tab stops set every 6 spaces.
//**
//*****************************************************************************
//*****************************************************************************
//**
//** File:		XPD.asm
//** Project:	XPD routines not included in the MASK ROM
//** Revised:	2/7/2006 1:27PM
//**
//** Description:	This file contains routines for debugging firmware using the
//**              XPD port.  These routines do not utilize semaphores to
//**              control access to the XPD port.
//**
//*****************************************************************************





//-------------------------------------------------------------------------------------------------
//	Subroutine: echo_dec_noLeadingZeroes
//		echos 16-bit number to terminal... takes number in R1,
//		uses R2 for temp, divisor, remainder...
//		subroutines use R0 as scratch
//-------------------------------------------------------------------------------------------------
echo_dec_noLeadingZeroes:
		st	r6,sp,0
		st	r2,sp,1
		st	r0,sp,2
		st	r4,sp,3
		add	sp,sp,4		//push r0

		mov	r4,-1
		//find 10000's digit
		mov	r2,10000
		jsr	r6,UnsignedDivide
		rol	r1,r1,0
		bc	ZS,echo_dec_special10
		mov	r4,0
		add	r1,r1,'0'
		jsr	r6,XPD_WriteByte	//echo result
	echo_dec_special10:
		//find 1000's digit
		mov	r1,0
		xor	r1,r1,r2		//copy remainder to r1
		mov	r2,1000
		jsr	r6,UnsignedDivide
		add	r1,r1,'0'
		rol	r4,r4,0		//test
		bc	ZS,echo_dec_special20skip
		sub	r0,r1,'0'
		bc	EQ,echo_dec_special20
	echo_dec_special20skip:
		mov	r4,0
		jsr	r6,XPD_WriteByte	//echo result
	echo_dec_special20:
		//find 100's digit
		mov	r1,0
		xor	r1,r1,r2		//copy remainder to r1
		mov	r2,100
		jsr	r6,UnsignedDivide
		add	r1,r1,'0'
		rol	r4,r4,0		//test
		bc	ZS,echo_dec_special30skip
		sub	r0,r1,'0'
		bc	EQ,echo_dec_special30
	echo_dec_special30skip:
		mov	r4,0
		jsr	r6,XPD_WriteByte	//echo result
echo_dec_special30:
		//find 10's digit
		mov	r1,0
		xor	r1,r1,r2		//copy remainder to r1
		mov	r2,10
		jsr	r6,UnsignedDivide
		add	r1,r1,'0'
		rol	r4,r4,0		//test
		bc	ZS,echo_dec_special40skip
		sub	r0,r1,'0'
		bc	EQ,echo_dec_special40
	echo_dec_special40skip:
		mov	r4,0
		jsr	r6,XPD_WriteByte	//echo result
	echo_dec_special40:
		//echo 1's digit
		mov	r1,0
		xor	r1,r1,r2		//remainder = 1's digit
		add	r1,r1,'0'
		jsr	r6,XPD_WriteByte	//echo result


		sub	sp,sp,4
		ld	r6,sp,0
		ld	r2,sp,1
		ld	r0,sp,2
		ld	r4,sp,3
		jsr	r6,r6			//return


XPD_ReadByte_masked:
		st	r6,sp,0
		add	sp,sp,1
		jsr	r6,XPD_ReadByte
		and	r1,r1,0x00FF
		sub	sp,sp,1
		ld	r6,sp,0
		jsr	r6,r6


XPD_ReadByteWithTimeout_masked:
		st	r6,sp,0
		add	sp,sp,1
		jsr	r6,XPD_ReadByteWithTimeout
		sub	sp,sp,1
		ld	r6,sp,0		
		rol	r1,r1,0
		bc	NS,XPD_ReadByteWithTimeout_masked10
		and	r1,r1,0x00FF
	XPD_ReadByteWithTimeout_masked10:
		jsr	r6,r6

